package com.tracy.life.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gc.flashview.FlashView;
import com.google.gson.Gson;
import com.tracy.life.R;
import com.tracy.life.adapter.Description_VpAdapter;
import com.tracy.life.bean.DescriptionBean;
import com.tracy.life.constant.UrlString;
import com.tracy.life.fragment.WebFragment;
import com.tracy.life.utils.OKHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class DescriptionActivity extends AppCompatActivity {

    @InjectView(R.id.iv_back)
    ImageView mIvBack;
    @InjectView(R.id.iv_share)
    ImageView mIvShare;
    @InjectView(R.id.fv_pic)
    FlashView mFvPic;
    @InjectView(R.id.tv_title)
    TextView mTvTitle;
    @InjectView(R.id.tv_price)
    TextView mTvPrice;
    @InjectView(R.id.tv_desc)
    TextView mTvDesc;
    @InjectView(R.id.ll_container)
    LinearLayout mLlContainer;
    @InjectView(R.id.ll_piccontainer)
    LinearLayout mLlPiccontainer;
    @InjectView(R.id.tv_favorite)
    TextView mTvFavorite;
    @InjectView(R.id.tab)
    TabLayout mTab;
    @InjectView(R.id.vp_bottom)
    ViewPager mVpBottom;
    private String mDescriptionUrl;
    private String tag = "tag";
    private List<Fragment> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        mDescriptionUrl = UrlString.getDescriptionUrl(id);
        initData();
    }

    private void initData() {
        mList = new ArrayList<>();
        OKHttpUtils.newInstance(this).getAsyncMainData(mDescriptionUrl, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                Toast.makeText(DescriptionActivity.this, "网络错误，加载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String result) {
                DescriptionBean descriptionBean = new Gson().fromJson(result, DescriptionBean.class);
                initView(descriptionBean);
            }
        }, tag);
    }

    private void initView(DescriptionBean descriptionBean) {
        DescriptionBean.DataBean dataBean = descriptionBean.getData();
        mFvPic.setImageUris(dataBean.getImage_urls());

        mTvTitle.setText(dataBean.getName());

        mTvPrice.setText("￥" + dataBean.getPrice());

        mTvDesc.setText(dataBean.getDescription());

        mTvFavorite.setText(dataBean.getPublic_favorites_count() + "个清单收录");

        mList.add(WebFragment.newInstance(dataBean.getDetail_html()));
        mVpBottom.setAdapter(new Description_VpAdapter(getSupportFragmentManager(), mList));

        mTab.setupWithViewPager(mVpBottom);
        mTab.getTabAt(0).setText("图文介绍");

        for (int i = 0; i < dataBean.getRelated_posts().size(); i++) {
            View itemView = LayoutInflater.from(this).inflate(R.layout.description_strip, null);
            TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_name.setText(dataBean.getRelated_posts().get(i).getTitle());
            mLlContainer.addView(itemView);
        }

//        for (int i = 0; i < dat; i++) {
//
//        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OKHttpUtils.newInstance(this).cancel(tag);
    }
}
